﻿package common.degrafa.render
{
	import com.degrafa.GeometryGroup;
	import com.degrafa.Surface;
	import com.degrafa.geometry.Geometry;
	import com.degrafa.geometry.Path;
	import com.degrafa.geometry.RasterText;
	import com.degrafa.paint.SolidFill;
	import com.degrafa.paint.SolidStroke;
	
	import common.AnyChartFlexExt;
	import common.LsMapUtil;
	import common.degrafa.event.SvgMapEvent;
	import common.degrafa.render.utils.SVGColor;
	import common.degrafa.render.utils.SVGParser;
	import common.degrafa.render.utils.SVGRender;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.FlexGlobals;
	import mx.effects.CompositeEffect;
	import mx.effects.Fade;
	import mx.effects.Sequence;
	
	import spark.effects.Rotate3D;

	public class SVGToGeometry extends SVGRender
	{
		[Bindable]
		public var gddwbm:uint = 0; //显示当前选中地区的id
		[Bindable]
		public var gddwmc:String = ""; //显示当前选中地区的name
		[Bindable]
		public var surface:Surface = new Surface(); //地图控件，提供给当前页面等加载的对象
		[Bindable]
		public var parent:Canvas;
		[Bindable]
		public var gddwArray:Array; //单位信息
		[Bindable]
		public var year:Number ;
		[Bindable]
		public var _personalize:int;	//定制类型
		public var geometricGroup:GeometryGroup = new GeometryGroup();
		public var parser:SVGParser;

		[Bindable]
		private var svgWidth:Number = 0;
		[Bindable]
		private var svgHeight:Number = 0;

		public function SVGToGeometry():void
		{
			//CursorManager.setCursor(zoomOutCursor);
		}

		public function cargar(parent:Canvas, geometricGroup:GeometryGroup, svg:Object, gddwArray:Array,year:Number,_personalize:int):Surface
		{
			this.parent = parent;
			this.parent.removeAllChildren();
			this.year = year;
			this._personalize = _personalize;
			this.gddwArray = gddwArray;
			if (svg is XML)
			{
				parser = new SVGParser(svg as XML);
				svg_object = parser.parse();
			}
			else if (svg is Object)
			{
				svg_object = svg;
			}
			geometricGroup.target = surface;
			svgWidth = parseFloat((svg_object.width + "").substring(0, (svg_object.width + "").length - 2));
			svgHeight = parseFloat((svg_object.height + "").substring(0, (svg_object.height + "").length - 2));
			surface.graphicsCollection.addItem(geometricGroup);
			visit(svg_object, geometricGroup);
			//surface.addChildAt(geometricGroup,0);
			return surface;
		}

		public function setMapXY():void
		{
			//设置地图的大小和位置
			var widthRadio:Number = FlexGlobals.topLevelApplication .width * 0.8 / svgWidth; // FlexGlobals.topLevelApplication .width
			var heightRadio:Number = FlexGlobals.topLevelApplication .height * 0.7 / svgHeight;
			this.geometricGroup.scaleX = widthRadio;
			this.geometricGroup.scaleY = heightRadio;
			this.geometricGroup.x = 50;
			this.geometricGroup.y = 40;
			var t1 :AnyChartFlexExt = null;
			var t :VBox  = null;
			 for(var i: int = 0;i<this.parent.getChildren().length;i++){
				if( this.parent.getChildren()[i] is VBox) {
					t = this.parent.getChildren()[i] as VBox;
					t.move(t.data.x * widthRadio+50,t.data.y * heightRadio+40);
				}else if( this.parent.getChildren()[i] is AnyChartFlexExt){
					heightRadio = FlexGlobals.topLevelApplication .height * 0.65 / svgHeight;
					t1 = this.parent.getChildren()[i] as AnyChartFlexExt;
					t1.setActualSize(t1.width * widthRadio, t1.height * heightRadio);
					t1.move(t1.data.x * widthRadio+50,t1.data.y * heightRadio+40);
				}
			}
		}

		override protected function personalizeObject(elt:Object, g:GeometryGroup, s:Geometry):void
		{
			var geo:DegrafaGeometry = new DegrafaGeometry(elt);
			geo.name = elt.id;
			g.addChildAt(geo, elt.id);

			geo.doubleClickEnabled = true;
			geo.addEventListener(MouseEvent.DOUBLE_CLICK, onMouseClick);
			if (elt.transform)
			{
				geo.transform.matrix = elt.transform;
			}
			if (elt.styleenv["display"] == "none")
			{
				geo.visible = false;
			}
			geo.geometryCollection.addItem(s);

			var color:uint;
			var data:Object = null;
			if(this._personalize == LsMapUtil._personalize1){
				var y:String = "";
				if(this.year == 2020){
					y = "2020";
				}
				if(s is Path){
					for (var k:Number = 0; k < gddwArray.length; k++){
						if (geo.gddwbm == gddwArray[k].gddw){
							color = SVGColor.parseToInt(LsMapUtil.color(gddwArray[k]['tdsj'+y]));
							s.fill = new SolidFill(color, 10);
							s.stroke = new SolidStroke(SVGColor.parseToInt("#EAE73D"),1,0.5);
						}
					}
				}
				if (s is RasterText){
					for (var i:Number = 0; i < gddwArray.length; i++){
						data = gddwArray[i];
						if (elt.gddw == data.gddw && data.gddw != '019900'){
							var vbox:VBox = new VBox();
							var fontColor:String="#000000";
							if(data.gddw=='099900'||data.gddw=='079900'){
								fontColor="#ffffff";
							}else{
								fontColor="#000000";
							}
							vbox.setStyle("verticalGap",-10);
							//设置x y
							var obj:Object = new Object();
							obj.x = elt.x;
							obj.y = elt.y;
							vbox.data = obj;
							vbox.x = elt.x;
							vbox.y = elt.y;
							
							var hbox1:HBox = new HBox();
							hbox1.setStyle("horizontalGap",0);
							//设置单位名称
							var dwmc:Text = new Text();
							dwmc.text = geo.gddwmc ;
							dwmc.setStyle("color",fontColor);
							dwmc.setStyle("fontWeight","bold");
							dwmc.setStyle("fontSize",14);
							dwmc.height = 30;
							hbox1.addChild(dwmc);
							if(data['zzl'+y] != null){
								//增长趋势
								var img:Image = new Image();
								if(data['zzqs'+y] > 0){
									img.source = "asset/images/b2.png";
								}else{
									img.source = "asset/images/b1.png";
								}
								//增长率
								var zzl:Text = new Text();
								zzl.setStyle("color",fontColor);
								zzl.setStyle("fontSize",18);
								zzl.text = data['zzl'+y] + "%";
								hbox1.addChild(img);
								hbox1.addChild(zzl);
							}
							
							var hbox2:HBox = new HBox();
							hbox2.setStyle("horizontalGap",0);
							//停电时间
							if(data['tdsj'+y] != null){
								var tdsj:Text = new Text();
								tdsj.text = data['tdsj'+y];
								tdsj.setStyle("color",fontColor);
								tdsj.setStyle("fontSize",18);
								//tdsj.setStyle("fontWeight","bold");
								var tdsjdw:Text = new Text();
								tdsjdw.text = "小时";
								tdsjdw.setStyle("color",fontColor);
								tdsjdw.setStyle("fontSize",12);
								tdsjdw.setStyle("paddingTop",5);
								hbox2.addChild(tdsj);
								hbox2.addChild(tdsjdw);
							}
							vbox.addChild(hbox1);
							vbox.addChild(hbox2);
							parent.addChild(vbox);
							
						}
					}
				}
			} else if(this._personalize == LsMapUtil._personalize2){
				if(s is Path){
					for (var k1:Number = 0; k1 < gddwArray.length; k1++){
						if (geo.gddwbm == gddwArray[k1].gddw){
							color = SVGColor.parseToInt(LsMapUtil.rankColor(gddwArray[k1]['rn']));
							s.fill = new SolidFill(color, 10);
							s.stroke = new SolidStroke(SVGColor.parseToInt("#EAE73D"),1,0.5);
						}
					}
				}
				if (s is RasterText){
					var config:Object = new Object();
					config.wdbm='gddw';
					config.zbbm=['gdl','sdl'];
					config.zbmc=['购电量','售电量'];
					for (var i1:Number = 0; i1 < gddwArray.length; i1++){
						data = gddwArray[i1];
						if (elt.gddw == data.gddw){
							var bar:AnyChartFlexExt = new AnyChartFlexExt();
							//设置x y
							var obj1:Object = new Object();
							obj1.x = elt.x;
							obj1.y = elt.y - 80;
							bar.data = obj1;
							bar.x = elt.x;
							bar.y = elt.y;
							bar.width = 94;
							bar.height = 130;
							//bar.alpha = 0.8;
							bar.setStyle("border",false);
							config.dwmc = geo.gddwmc;
							config.xsl = data.xsl;
							config.maximumOffset = "0.5";
							bar.anychartXML = LsMapUtil.parseBarXml(config,data);
							parent.addChild(bar);
							
							//var _3d:Rotate3D = new Rotate3D();
							//_3d.angleXTo = 15;
							//_3d.target = bar;
							//_3d.duration =3000;
							//_3d.play();
						}
					}
				}
			}
			
		}

		protected function onMouseClick(e:MouseEvent):void
		{ //1下钻
			var dg:DegrafaGeometry = e.target as DegrafaGeometry;
			var temp:GeometryGroup = dg.parent as GeometryGroup;
			this.dispatchEvent(new SvgMapEvent(SvgMapEvent.MAP_DRILLDOWN, dg.gddwbm, dg.gddwmc)); //get map file param
			var seq:CompositeEffect = new Sequence();
			var fadeOut:Fade = new Fade();
			fadeOut.duration = 700;
			fadeOut.alphaFrom = 1.0;
			fadeOut.alphaTo = 0.0;

			var fadeIn:Fade = new Fade();
			fadeIn.duration = 700;
			fadeIn.alphaFrom = 0.0;
			fadeIn.alphaTo = 1.0;

			seq.addChild(fadeOut);
			seq.addChild(fadeIn);

			for (var i:Number = 0; ; i++)
			{
				var tempDg:DegrafaGeometry = dg.parent.getChildAt(i) as DegrafaGeometry;
				if (tempDg == null)
				{
					break;
				}
				if (dg.gddwmc == tempDg.gddwmc)
				{
					seq.target = tempDg;
					seq.play();
					break;
				}
			}
			try
			{
				gddwbm = Number(dg.gddwbm);
			}
			catch (e:Error)
			{
				Alert.show("id is not number");
			}
		}

	}
}
